package deployr.core;

import static deployr.core.Slurp.*;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class DiscoverVersions {

	private List<String> versions = new LinkedList<String>();

	public void parse(InputStream inputStream) {
		String string = slurp(inputStream);
		Scanner scanner = new Scanner(string).useDelimiter("\n");
		while (scanner.hasNext())
			parse(scanner.next());
	}

	public void parse(String string) {
		string = string.replaceAll("\r$", "");

		String regex = ".*<a href=\"(.*)/\">(.*)/</a.*";

		if (!string.matches(regex)) {
			return;
		}

		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(string);

		if (!m.matches()) return;

		String s1 = m.group(1);
		String s2 = m.group(2);

		if (s1.equals(s2)) versions.add(s1);
	}

	public Iterable<String> versions() {
		return versions;
	}

}